# By daoyu @2010-07-18

from django import template
from django.contrib.auth.models import User
   
from uncle.users.models import *
from uncle.util.util import *

register = template.Library()   
   
#-------------------------------------------------------------------------------
# Return the upload requests number for artist.
#-------------------------------------------------------------------------------
@register.filter('count_up_req')   
def count_up_req(value):    
    return len(Upload_request.objects.filter(user__id = value))

@register.filter('get_file_name')   
def get_file_name(value):
    return str(value).split('.')[0]

@register.filter('get_project_file_img')   
def get_project_file_img(value):
    if is_video(value):
        return str(value).split('.')[0] + '.jpg'
    elif is_picture(value):
        return value;
    
@register.filter('get_video_name')   
def get_video_path(value):
    return '//upload//vedio//'+ value.id+'//'+str(value.name).split('.')[0]

@register.filter('get_user')   
def get_user_path(value):
    return 'upload/user/'+ str(value.id)

#-------------------------------------------------------------------------------
# Give /path1/path2/path3/a.txt
# Return a.txt as file name
#-------------------------------------------------------------------------------
@register.filter('download_file_name')   
def download_file_name(value):
    if str(value).find('/') == -1:
        return str(value)
    else:       
        filename = str(value).split('/')[-1]
        return filename

@register.filter('play_in_player')
def play_in_player(value):
    vi = ['mp4','mov','3gp','flv','jpg','gif','png','bmp','tif']
    
    for v in vi:
        if value.file.name.lower().endswith(v):
            return '<a href="?m='+ str(value.id) +'"><img height="90" src="/'+ value.file.name +'_.jpg"></a> <br/>'
        
    return '<a href="/'+ value.file.name +'"><img height="90" src="/'+ value.file.name+'_.jpg"></a> <br/>'